package eu.jakubiak.ioc
{
	import eu.jakubiak.ioc.beans.Abra;
	import eu.jakubiak.ioc.beans.Cadabra;
	import eu.jakubiak.ioc.beans.Hocus;
	import eu.jakubiak.ioc.beans.Pocus;
	import eu.jakubiak.ioc.beans.Presto;
	
	import flexunit.framework.TestCase;

	public class IocTest extends TestCase
	{
		public function IocTest(methodName:String=null)
		{
			super(methodName);
		}
		

		/**
		 * Testing for unnamed beans
		 */		
		public function testAbracadabra():void {
			var abra:Abra = new Abra();
			var cadabra:Cadabra = new Cadabra();
			
			var ioc:Ioc = new Ioc();
			
			ioc.manage(abra);
			ioc.manage(cadabra);
			
			assertNotNull(abra.cadabra);
			assertNotNull(cadabra.abra);
			
			assertEquals(cadabra, abra.cadabra);
			assertEquals(abra, cadabra.abra);
			
		}
		
		/**
		 * Testing for named beans
		 */
		public function testFocuspocus():void {
			var hocus:Hocus = new Hocus();
			var pocus:Pocus = new Pocus();
			
			var ioc:Ioc = new Ioc();
			
			ioc.manage(hocus);
			ioc.manage(pocus);
			
			assertNotNull(hocus.pocus);
			assertNotNull(pocus.hocus);
			
			assertEquals(hocus,pocus.hocus);
			assertEquals(pocus,hocus.pocus);		
		}
		
		/**
		 * Testing for primitive types
		 */
		public function testPresto():void {
			var presto:Presto = new Presto();
			
			var ioc:Ioc = new Ioc();
			
			ioc.manage(new Number(12345), "myNumber");
			ioc.manage(presto);
			ioc.manage("Hello World", "myString");
			
			assertNotNull(presto.string);
			assertNotNull(presto.number);

			assertEquals("Hello World", presto.string);
			assertEquals(new Number(12345), presto.number);			
						
		}
		
		public function testBidirection():void {
			var abra1:Abra = new Abra();
			
			
			var ioc:Ioc = new Ioc();
			
			ioc.manage(abra1);

			var cadabra1:Cadabra = new Cadabra();
			abra1.cadabra = cadabra1;

			
			var abra2:Abra = new Abra();
			ioc.manage(abra2);
			
			assertNotNull(abra2.cadabra);
			
			
			var cadabra2:Cadabra = new Cadabra();
			
			abra1.cadabra = cadabra2;
			assertEquals(cadabra2, abra1.cadabra);
			
			assertEquals(cadabra2, abra2.cadabra);
			
		}
		
	}
}